package com.benchmark.springboot.error;

import com.benchmark.springboot.exception.APIException;
import com.power.common.model.CommonResult;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.reflect.MethodSignature;

import java.lang.reflect.Method;

/**
 * 仅仅是参考，不启用
 * @author yu 2021/10/31.
 */
@Slf4j
public class GlobalErrorHandler {

    @Around(value = "execution(public * com.benchmark.springboot.controller..*.*(..))")
    public Object around(ProceedingJoinPoint joinPoint) {
        try {
            MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
            Method method = methodSignature.getMethod();
            log.info("method Name:{}", method.getName());
            Object result = joinPoint.proceed(joinPoint.getArgs());
            return result;
        } catch (APIException apiException) {
            log.error("[GlobalErrorHandler]: 出现自定义异常 ApiException = {}", apiException.getMessage());
            return CommonResult.fail(apiException.getCode(),apiException.getMessage());
        } catch (Throwable throwable){
            log.error(throwable.getMessage(), throwable);
        }
        return null;
    }
}
